<template>
  <div class="search-histort">
    <van-cell title="搜索历史">
      <div v-if="isDeleteShow">
        <span @click="$emit('clear-history')">全部删除</span>
        <span @click="isDeleteShow = false" class="text">完成</span>
      </div>
      <van-icon name="delete" @click="isDeleteShow = true" v-else/>
    </van-cell>
    <van-cell :title="item" v-for="(item, index) in historyList" :key="index" @click="deleteHistory(item, index)">
      <van-icon name="close" />
    </van-cell>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isDeleteShow: false // 删除图标的显示状态
    }
  },
  props: {
    historyList: {
      type: Array,
      required: true
    }
  },
  created() {},
  mounted() {},
  methods: {
    // 点击删除历史记录
    deleteHistory(item, index) {
      if (this.isDeleteShow) {
        // 删除
        this.historyList.splice(index, 1)
      } else {
        // 搜索
        this.$emit('search-text', item)
      }
    }
  }
}
</script>

<style scoped lang="less">
.search-histort {
  .text {
    margin-left: 15px;
  }
}
</style>
